import SnakeMod from "../../model/SnakeMod";
import BaseView from "../../core/mvc/BaseView";
import { ClientEvent } from "../../event/ClinetEvent";

const { ccclass, property } = cc._decorator;
const et = ClientEvent.Type

@ccclass
export default class UIRank extends BaseView {

    /** 名称节点 */
    @property(cc.Node)
    nameNode: cc.Node = null
    /** 分数节点 */
    @property(cc.Node)
    scoreNode: cc.Node = null

    nameLabels: Array<cc.Label> = []
    scoreLabels: Array<cc.Label> = []

    _mod: SnakeMod

    // onLoad () {}

    start() {
        this._mod = SnakeMod.ins

        this.nameNode.children.forEach(n =>
            this.nameLabels.push(n.getComponent(cc.Label)))
        this.scoreNode.children.forEach(n =>
            this.scoreLabels.push(n.getComponent(cc.Label)))

        this.ed.on(et.RANK_UPDATE, this, e => {
            let rankArr = e.rankArr
            rankArr.forEach((r, i) => {
                let snake = this._mod.getSnake(r.id)
                snake ? this.nameLabels[i].string = snake.name : ""
                this.scoreLabels[i].string = Math.floor(r.score).toString()
            })
        })
    }
}
